#include "../Header Files/CMainMenuState.h"
#include "../Header Files/CIntroCinematicState.h"
#include "../Header Files/CMainMenu_PlayState.h"
#include "../Header Files/CMainMenu_LoadState.h"
#include "../Header Files/CMainMenu_OptionsState.h"
#include "../../Renderer/Header Files/CRenderer_AL.h"
#include "../../Renderer/Header Files/CAssetManager_AL.h"
#include "../../Renderer/Header Files/Direct3D_AL.h"
#include "../Header Files/CMainMenuState.h"
#include "../../Misc/CDirectInput.h"

enum CHOICES_MAINMENU{ PLAY_MM, LOAD_MM, OPTIONS_MM, EXIT_MM };
void CMainMenuState::Update(float fDT)
{
	//Handle Updating
	//Handle Input
	Input();
	if (m_pCursor->GetPosition().y != m_vCursorPositions[m_nCurrentCursorPosition].y)
	{
		D3DXVECTOR3 pos = m_pCursor->GetPosition();
		if (pos.y < m_vCursorPositions[m_nCurrentCursorPosition].y)
			pos.y += 240 * fDT;
		else
			pos.y -= 240 * fDT;
		m_pCursor->SetPosition(pos);
	}
	m_pObjectManager->Update(fDT, m_pCamera);
}
void CMainMenuState::Exit()
{
	m_pObjectManager->ClearAllObjects();
	if (m_pCamera)
	{
		delete m_pCamera;

	}
}
void CMainMenuState::Enter()
{
	m_pObjectManager->AddItemToObjects(m_pBGSprite);
	m_pObjectManager->AddItemToObjects(m_pCursor);


	int Win_Height = Direct3D_AL::GetInstance()->m_nWindowHeight;
	int Win_Width = Direct3D_AL::GetInstance()->m_nWindowWidth;
	int lineCounter = 0;
	m_nCurrentCursorPosition = 0;

	m_Text.PrintTextKerned("Main Menu", D3DXVECTOR3(Win_Width * 0.5f - (120), 0, 0), D3DXVECTOR3(1.0f, 1.0f, 1.0f));
	m_Text.PrintTextKerned("Start", D3DXVECTOR3(Win_Width * 0.4f, Win_Height * 0.5f + m_nTextHeight * lineCounter, 0), D3DXVECTOR3(1.0f, 1.0f, 1.0f));
	lineCounter++;
	m_Text.PrintTextKerned("Load", D3DXVECTOR3(Win_Width * 0.4f, Win_Height * 0.5f + m_nTextHeight * lineCounter, 0), D3DXVECTOR3(1.0f, 1.0f, 1.0f));
	lineCounter++;
	m_Text.PrintTextKerned("OPtions", D3DXVECTOR3(Win_Width * 0.4f, Win_Height * 0.5f + m_nTextHeight * lineCounter, 0), D3DXVECTOR3(1.0f, 1.0f, 1.0f));
	lineCounter++;
	m_Text.PrintTextKerned("Exit", D3DXVECTOR3(Win_Width * 0.4f, Win_Height * 0.5f + m_nTextHeight * lineCounter, 0), D3DXVECTOR3(1.0f, 1.0f, 1.0f));
	

}
void CMainMenuState::Initialize()
{
	m_pCamera = new cCamera_AL();
	m_pObjectManager = CObjectManager::GetInstance();
	int id = CAssetManager_AL::GetInstance()->CreateTexture("MainMenuTextures//MainMenu_BackgroundTexture.png");
	RECT r = { 0, 0, 2000, 1000 };
	CSpriteNode_AL* temp = new CSpriteNode_AL(CAssetManager_AL::GetInstance()->GetTexture(id), r, D3DXVECTOR3(0, 0, 0), D3DXVECTOR3(0.5f, 0.5f, 1.0f));
	m_pBGSprite = new CBaseObject(temp);
	
	//todo : modulate this to modulated form of loading fonts
	m_Text.LoadBasicText();
	m_Text.SetHeight(45);
	m_Text.SetWidth(1);
	m_nTextHeight = 75;
	//end todo

	///
	//store position data for selections
	int Win_Height = Direct3D_AL::GetInstance()->m_nWindowHeight;
	int Win_Width = Direct3D_AL::GetInstance()->m_nWindowWidth;
	int lineCounter = 0;
	m_nCurrentCursorPosition = 0;
	m_vCursorPositions.push_back(D3DXVECTOR3(Win_Width * 0.4f - 64, Win_Height * 0.5f + m_nTextHeight * lineCounter, 0));
	lineCounter++;
	m_vCursorPositions.push_back(D3DXVECTOR3(Win_Width * 0.4f - 64, Win_Height * 0.5f + m_nTextHeight * lineCounter, 0));
	lineCounter++;
	m_vCursorPositions.push_back(D3DXVECTOR3(Win_Width * 0.4f - 64, Win_Height * 0.5f + m_nTextHeight * lineCounter, 0));
	lineCounter++;
	m_vCursorPositions.push_back(D3DXVECTOR3(Win_Width * 0.4f - 64, Win_Height * 0.5f + m_nTextHeight * lineCounter, 0));


	id = CAssetManager_AL::GetInstance()->CreateTexture("MainMenuTextures//MainMenu_Cursor.png");
	r = { 0, 0, 32, 32 };
	temp = new CSpriteNode_AL(CAssetManager_AL::GetInstance()->GetTexture(id), r, m_vCursorPositions[0], D3DXVECTOR3(1.0f, 1.0f, 1.0f));
	m_pCursor = new CBaseObject(temp);
	m_pCursor->SetPosition(m_vCursorPositions[m_nCurrentCursorPosition]);
	m_pCursor->SetFactionType(2);
	
}
bool CMainMenuState::Input()
{
	CDirectInput::GetInstance()->ReadDevices();
	if (CDirectInput::GetInstance()->KeyPressedEx(DIK_DOWN))
	{
		m_nCurrentCursorPosition++;
		if (m_nCurrentCursorPosition > 3)
			m_nCurrentCursorPosition = 0;

		std::cout << m_nCurrentCursorPosition << std::endl;
	}
	else if (CDirectInput::GetInstance()->KeyPressedEx(DIK_UP))
	{
		m_nCurrentCursorPosition--;
		if (m_nCurrentCursorPosition < 0)
			m_nCurrentCursorPosition = 3;
		std::cout << m_nCurrentCursorPosition << std::endl;
	}
	else if (CDirectInput::GetInstance()->KeyPressedEx(DIK_RETURN))
	{
		switch (m_nCurrentCursorPosition)
		{
			case PLAY_MM:
			{
				CMainMenu_PlayState* state = new CMainMenu_PlayState();
				state->Initialize();
				CGame::GetInstance()->PopState();
				CGame::GetInstance()->PushState(state);
			}
		break;
			case LOAD_MM:
			{
			}
		break;
			case OPTIONS_MM:
			{
			}
		break;
			case EXIT_MM:
			{
			}
		break;
		}
	}
	return true;
}